﻿using WH.Contracts.Systems.Roles;
using WH.Model.Systems;
using WH.Repositories.IRepositories.ISystem;
using WH.Services.BaseService;
using WH.Services.IServices.ISystem;
using Rw.Core.Common.Paging;
using WH.Common.Paging;
using SqlSugar;

namespace WH.Services.Services.System
{
    public class RoleService : BaseService<RoleEntity>, IRoleService
    {
        private readonly IRoleRepository _roleRepository;
        public RoleService(IRoleRepository roleRepository) : base(roleRepository)
        {
            _roleRepository = roleRepository;
        }
        /// <summary>
        /// 分页数据
        /// </summary>
        /// <param name="pager"></param>
        /// <param name="filter"></param>
        /// <returns></returns>
        public async Task<IPagedList<RoleEntity>> GetRolesByPageAsync(PageParam pager, RoleFilter filter)
        {
            var query = Expressionable.Create<RoleEntity>();
            if (!string.IsNullOrWhiteSpace(filter.Keyword))
            {
                filter.Keyword = filter.Keyword.Trim();
                query.And(map => map.Code.Contains(filter.Keyword) || map.Name.Contains(filter.Keyword));
            }

            if (!string.IsNullOrWhiteSpace(filter.Code))
            {
                filter.Code = filter.Code.Trim();
                query.And(map => map.Code.Contains(filter.Code));
            }

            if (!string.IsNullOrWhiteSpace(filter.Name))
            {
                filter.Name = filter.Name.Trim();
                query.And(map => map.Name.Contains(filter.Name));
            }

            if (!string.IsNullOrWhiteSpace(filter.OrganizationId))
            {
                filter.OrganizationId = filter.OrganizationId.Trim();
                query.And(map => map.OrganizationId.Contains(filter.OrganizationId));
            }

            if (filter.Status.HasValue)
            {
                query.And(map => map.IsEnable == filter.Status.Value);
            }

            var paged = await _roleRepository.PagedFindAllAsync(query.ToExpression(), pager);
            return paged;
        }
    }
}
